<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: Cruise <jiangyu@hrbjjhh.com>
// +----------------------------------------------------------------------------

/**
 *@todo 客诉模型
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2016-04-14
 +------------------------------------------------------------------------------
 */
namespace Admin\Model;
use Admin\Model\BaseModel;

class CustomerServiceModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
    	array('shop_id', 'checkId', '必须选择店铺', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    	array('customer_service_type', 'checkId', '必须选择投诉分类', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    	array('customer_service_status', 'checkId', '必须选择处理状态', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
    	array('create_time', NOW_TIME, self::MODEL_INSERT),
    	array('update_time', NOW_TIME, self::MODEL_UPDATE),
    	array('uid',UID, self::MODEL_BOTH),
    	array('status',1, self::MODEL_BOTH)
    );
    
    /**
     * @todo 检测客诉品类是否选中
     * @param  string $id 商户品类id
     * @return boolean     ture - id成立，false - id无效
     */
    protected function checkId($id){
    	$id = (int)$id;
    	if($id == 0 || $id == ''){
    		return false;
    	}else{
    		return true;
    	}
    }
    
    /**
     * @todo 取得客诉报表数据
     * @param array
     * @return array null
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getCustomerComplain($map = null,$field = true){
	    if(!is_array($map) || empty($map)){
	    	return null;
	    }
    	$list = $this->where($map)->field($field)->select();
    	if(empty($list)){
    		return null;
    	}
    	//初始化键
    	$arr = array(11=>null,12=>null,'other'=>null,'count'=>null);
    	//初始化返回值
    	$return_list = array();
    	foreach ($list as $key=>$val){
    		if(!isset($return_list[$val['uid']][$val['customer_service_status']])){
    			$return_list[$val['uid']] = array(1=>$arr,2=>$arr,3=>$arr);
    		}
    		if($val['customer_service_type'] == 11){
    			$return_list[$val['uid']][$val['customer_service_status']][11] += 1;
    		}elseif($val['customer_service_type'] == 12){
    			$return_list[$val['uid']][$val['customer_service_status']][12] += 1;
    		}else{
    			$return_list[$val['uid']][$val['customer_service_status']]['other'] += 1;
    		}
    		$return_list[$val['uid']][$val['customer_service_status']]['count'] = $return_list[$val['uid']][$val['customer_service_status']][11]+
    																			  $return_list[$val['uid']][$val['customer_service_status']][12]+
    																			  $return_list[$val['uid']][$val['customer_service_status']]['other'];
    	}
    	foreach($return_list as $key=>$val){
    		$return_list[$key][11] = $val[1][11]+$val[2][11]+$val[3][11];
    		$return_list[$key][12] = $val[1][12]+$val[2][12]+$val[3][12];
    		$return_list[$key]['other'] = $val[1]['other']+$val[2]['other']+$val[3]['other'];
    		$return_list[$key]['count'] = $return_list[$key][11]+$return_list[$key][12]+$return_list[$key]['other'];
    	}
     	return $return_list;
    }
    
    /**
     * @todo 取得店铺报表数据
     * @param array
     * @return array null
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getShopComplain($map = null,$field = true){
    	if(!is_array($map) || empty($map)){
    		return null;
    	}
    	$list = $this->where($map)->field($field)->select();
    	if(empty($list)){
    		return null;
    	}
     	foreach ($list as $key=>$val){
	      	$data_return[$val['shop_id']]['merchant_id'] = $val['merchant_id'];
	      	$data_return[$val['shop_id']]['shop_id'] = $val['shop_id'];
	      	if(!isset($data_return[$val['shop_id']]['type'])){
	      		$data_return[$val['shop_id']]['type'] = array (
	      				1=>null,2=>null,3=>null,4=>null,5=>null,
	      				6=>null,7=>null,8=>null,9=>null,12=>null);
	      	}
	      	$data_return[$val['shop_id']]['type'][$val['customer_service_type']] += 1;
     	}
     	foreach ($data_return as $k=>$v){
     		$data_return[$k]['count'] = array_sum($v['type']);
     	}
    	return list_sort_by($data_return,'count','desc');
    }
    
    /**
     * @todo 取得公司报表数据
     * @param array
     * @return array null
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getCompanyComplain($map = null,$field = true){
    	if(!is_array($map) || empty($map)){
    		return null;
    	}
    	$data = $this->where($map)->field($field)->select();
    	if(empty($data)){
    		return null;
    	}
    	//初始值
    	$data_return = array(13=>null,14=>null,15=>null,16=>null,17=>null);
    	foreach ($data as $key=>$val){
    		$data_return[$val['customer_service_type']] += 1;
    	}
    	$data_return['count'] = array_sum($data_return);
    	$data_return['activity_id'] = isset($map['activity_id']) ? $map['activity_id'] : null;
    	return $data_return;
    }
    
    /**
     * @todo 返回最后一次错误信息
     * @return string
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getError(){
    	return $this->error;
    }
    
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getPk(){
    	return 'customer_service_id';
    }
    
}
